<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

if( !function_exists('insert_at_interval')){
	define('START_FRONT', 0);
	define('START_BACK', 1);
	function insert_at_interval( $insert, $string, $interval = 1, $start_at = START_FRONT){
		if( is_string($string) && is_numeric($interval)){
			$interval = intval($interval);
			if( $start_at == START_BACK){
				$string = strrev($string);
			}
			$pieces = str_split($string, $interval);
			$new_string = implode($insert, $pieces);
			if( $start_at = START_BACK){
				$new_string = strrev($new_string);
			}
			return $new_string;
		}
		return null;
	}
}

if( !function_exists('format_email_display')){
	function format_email_display($email, $replace = ' [at] '){
		$email = preg_replace('/@/', $replace, $email);
		return $email;
	}
}

if( !function_exists('format_textarea')){
	function format_textarea($text, $q = ENT_QUOTES, $char = 'UTF-8'){
		return preg_replace('|(https?://[A-z0-9%-_?=&\.;]+)|', '<a target="_blank" href="$1">$1</a>', nl2br(htmlspecialchars($text, $q, $char)));
	}
}

if( !function_exists('highlight')){
	function highlight(&$result, $fields, $words){
		foreach($fields as $f){
			if( isset($result[$f])){
				foreach($words as $w){
					$result[$f] = preg_replace("/($w)/i", '<span class="highlight">$1</span>', $result[$f]);
					//$result[$f] = str_ireplace($w, '<span class="highlight">' . $w . '</span>', $result[$f]);
				}
			}
		}
	}
}
